package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.projectSetup.forms.ProjectSetupForm;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.dao.UserDefinedFieldsDAO;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.forecastEntry.dao.ForecastEntrySearchDAO;
import com.scalar.forecast.salesPersonSetup.dao.SalesPersonSetupDAO;
import com.scalar.chiptrack.sales.dao.CustomerRelationshipDAO;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.ArrayList;


/**
 * Process the request to display the project setup details which are present in the scope
 *
 * Created by Veeru.
 * Date: Feb 15, 2005
 * Time: 8:30:24 PM 
 */

public class ProjectSetupEditCommand extends ActionCommand {
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "projectSetup" );

        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        ProjectSetupForm projectSetupForm = ( ProjectSetupForm )form;
        ProjectSetup projectSetup = projectSetupForm.getProjectSetup();
        try {
            ProjectSetup dbProjectSetup = new ProjectSetup();
            dbProjectSetup.setProjectID( projectSetup.getProjectID() );
            ProjectSetupDAO.loadProjectSetupDetails( null, dbProjectSetup );
            request.setAttribute( "customers", ForecastEntrySearchDAO.getAllCustomers( null ) );
            request.setAttribute( "odmsList", CustomerRelationshipDAO.getOdmsForChangeSalesOrder( projectSetup.getCustomerID()+"", dbProjectSetup.getOdmID()+"" ) );
            request.setAttribute( "endUserList", CustomerRelationshipDAO.getEndCustomersForChangeSalesOrder( projectSetup.getCustomerID()+"", projectSetup.getEndUserId()+"" ) );
            request.setAttribute( "salesPersons", SalesPersonSetupDAO.getSalesPersonList() );
            ArrayList newUserDefinedfields = UserDefinedFieldsDAO.loadNewUserDefinedFieldValues( null, projectSetup.getProjectID(), projectSetup.getUserDefinedFeilds() );
            projectSetup.setUserDefinedFeilds( newUserDefinedfields );
            ProjectSetupDAO.convertPartNumberArraysToList( null, projectSetup );
        } catch ( Exception e ) {
            e.printStackTrace();
            errors.put( "Error while loading the Project Setup details", null );
        }

        request.setAttribute( "availableChipsList" , projectSetup.getAvailableChipsList() );
        request.setAttribute( "availableBoardsList" , projectSetup.getAvailableBoardsList() );
        request.setAttribute( "availableKitsList" , projectSetup.getAvailableKitsList() );

        request.setAttribute( "selectedChipsList" , projectSetup.getSelectedChipsList() );
        request.setAttribute( "selectedBoardsList" , projectSetup.getSelectedBoardsList() );
        request.setAttribute( "selectedKitsList" ,  projectSetup.getSelectedKitsList() );

        return actionForward;
    }

}
